<?php
/**
 * Created by PhpStorm.
 * User: 68008000
 * Date: 2017/11/23
 * Time: 10:34
 */

namespace app\admin\controller;


use houdunwang\arr\Arr;
use think\Controller;

/**
 * Class Goods
 * @package app\admin\controller
 * 商品控制器
 */
class Goods extends Controller
{
    protected $db;
    public function __construct()
    {
        parent::__construct();
        $this->db = new \app\common\model\Goods();
    }
    //商品主页
    public function index(){
        $data = db('goods')->order('time desc')->paginate(8);
        $this->assign('data',$data);
        //视图
        return view();
    }
    //商品添加
    public function add(){
        //获取所属分类
        $categoryData=db('category')->select();
        $treeCategoryData=Arr::tree($categoryData,'cname','cid','pid');
        $this->assign('treeCategoryData',$treeCategoryData);
        //2.处理所属品牌
        $brandData = db('brand')->select();
        $this->assign('brandData',$brandData);
        //商品添加
        if(request()->isPost()){
            $data=input('post.');
            $res=$this->db->add($data);
            if ($res['valid']) {
                $this->success($res['msg'],'index');
            } else {
                $this->error($res['msg']);
            }
        }
        //视图
        return view();
    }
    //获取tid获取数据
    public function getAttr(){
        //根据tid选择类型属性
        $tid=input('post.tid');
        $typeAttributeData=db('type-attribute')->where('tid',$tid)->select();

        foreach ($typeAttributeData as $k=>$v){
            $typeAttributeData[$k]['tavalue'] = explode(',',$v['tavalue']);
        }
        echo json_encode($typeAttributeData);
    }
    //商品编辑
    //编辑
    public function edit(){
        $gid = input('param.gid');
        //处理所属分类
        $cateData = Arr::tree(db('category')->select(),'cname');
        $this->assign('cateData',$cateData);
        //处理所属品牌
        $brandData = db('brand')->select();
        $this->assign('brandData',$brandData);
        //获取旧数据
        $oldData = db('goods')
            ->alias('g')
            ->join('__POG__ p','g.gid = g.gid')
            ->find($gid);
        $this->assign('oldData',$oldData);
        //商品属性处理
        //通过类型id获得对应的类型属性
        $typeAttr = db('type-attribute')
            ->where('tid',$oldData['tid'])
            ->where('class',2)
            ->select();;
        foreach ($typeAttr as $k=>$v){
            $typeAttr[$k]['tavalue'] = explode(',',$v['tavalue']);
        }
        $this->assign('typeAttr',$typeAttr);
        //通过商品属性表获得选中的属性
        $checkAttr = db('goods-attribute')
            ->where('gid',$gid)
            ->column('gtvalue');
        $this->assign('checkAttr',$checkAttr);

        //类型属性处理  类型属性表和商品属性表进行关联
        $specData = db('type-attribute')
            ->alias('ta')
            ->join('__GOODS-ATTRIBUTE__ ga','ta.taid = ga.taid')
            ->where('ta.class',1)
            ->where('ga.gid',$gid)
            ->select();
        foreach ($specData as $k=>$v){
            $specData[$k]['tavalue'] = explode(',',$v['tavalue']);
        }
        $this->assign('specData',$specData);
        //商品编辑
        if(request()->isPost()){
            $data=input('post.');
            $res=$this->db->edit($data);
            if ($res['valid']) {
                $this->success($res['msg'],'index');
            } else {
                $this->error($res['msg']);
            }
        }
        return view();
    }
    public function del($gid){
        $res = $this->db->del($gid);
        if ($res['valid']){
            $this->success($res['msg']);
        }else{
            $this->error($res['msg']);
        }
    }
}